<?php 
namespace Admin\Controller;
use Think\Controller;
class AuthController extends CommonController{
    //用户列表
    public function index(){
        $info=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group')->field('id,title')->select();
		$personnel_list=personnellist();//员工列表
		$this->assign('personnel_list',$personnel_list);
        $this->assign('groupInfo',$info);
        $this->display();
    }
	//基本设置
    public function baseConfig(){
		
        $this->display();
    }
	//密码设置
	public function passwordset(){
		$ck=A('CheckInput');
		$m=M('AuthUser',C('DB_PRS'),C('DB_DEPOT'));
		$username=I('username');
		$old_password=$ck->in('原密码','old_password','password','',5,20);	
		$new_password=$ck->in('新密码','new_password','password','',5,20);	
		$confirm_new_password=$ck->in('确认密码','confirm_new_password','password','',5,20);	

		$info=$m->where(array('username'=>$username,'password'=>$old_password))->find();
		if(empty($info)){
			$return['status']=false;
			$return['message']="原密码错误！";
			$this->ajaxReturn($return);
			exit;
		}else if($new_password<>$confirm_new_password){
			$return['status']=false;
			$return['message']="两次输入密码不一致，请重新输入！";
			$this->ajaxReturn($return);
			exit;
		}else{
		
			$data['password']=$new_password;
			$res=$m->where(array('username'=>$username))->save($data);//更新
			if($res!==false){
				$return['status']=false;
				$return['message']="修改成功！";
				$this->ajaxReturn($return);
			}else{
				$return['status']=false;
				$return['message']="修改失败！";
				$this->ajaxReturn($return);
			}
		}
		
		
		
	}
	//基本设置列表
	public function get_config(){
        $m=M('Config',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');

        //搜索条件
        $map=array();
        $name=$ck->in('名称','name','cnennumstr','',0,0);        
        $value=$ck->in('值','value','cnennumstr','',0,0);
		$status=I('status');

        !empty($name)?$map['name']=array('like','%'.$name.'%'):'';
		!empty($value)?$map['value']=array('like','%'.$value.'%'):'';
		if($status){
			if($status==1){
				$map['status']=1;
			}
			if($status==2){
				$map['status']=0;
			}
		}
		
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  

        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
        
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->order($sort.' '.$order)->select();
        }
		foreach($info as $k=>$v){
			$info[$k]['add_time']=date("Y-m-d h:i:s",$v['add_time']);
			if($v['status']==0){
				$info[$k]['status']='否';
			}
			if($v['status']==1){
				$info[$k]['status']='是';
			}
		}
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
    }
	//添加配置信息
    public function baseConfigAdd(){
		$m=M('Config',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');  
		$data['en_name']=I('en_name');  
        $data['value']=I('value');  
        $data['name']=I('name');  
		$data['add_time']=time();
        $insertId=$m->add($data);

        if(!$insertId){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }

    //更新配置信息
    public function baseConfigSave(){
		$m=M('Config',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['en_name']=I('en_name');  
        $data['value']=I('value');  
        $data['name']=I('name');  
		$data['update_time']=time();
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
    public function group(){        
        $this->display();
    }

    public function rule(){
        //dump($isAuth);
		$m=M('AuthRule',C('DB_PRS'),C('DB_DEPOT'));
        $authRuleInfo=$m->where(array('status'=>'1'))->field('id,pid,title')->order('o')->select();//查询模块信息 pid=0为模块,否则为规则标识
		// dump($authRuleInfo);
		// die;
        $this->assign('authRuleInfo',$authRuleInfo);
        $this->display();
    }

    public function userList(){
        $m=M('auth_user',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
        $name='';
        $email='';
        $name=$ck->in('用户名','name','cnennumstr','',0,0);        
        $email=$ck->in('邮箱','email','email','',0,0);
        $title=$ck->in('角色','title','cnennumstr','',0,0);
        !empty($name)?$map['username']=array('like','%'.$name.'%'):'';
        !empty($email)?$map['email']=$email:'';
        !empty($title)?$map['title']=$title:'';

        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  

        //排序
        $sort=$ck->in('排序','sort','cnennumstr','uid',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
        
        $count=$m->where($map)->cache('authAserList'.$name.$email.$title.$rows,60)->count();
        if(empty($map)){
            $info=$m->alias('AU')->join(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group_access as AGA on AGA.uid=AU.uid','left')->join(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group as AG on AG.id=AGA.group_id','left')->field('AU.uid,AU.username as name,AU.email,AU.nick_name,AU.lastloginip,AU.lastlogintime,AU.personnel_no,AU.realname,AU.score,AG.title,AG.id as groupId')->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->alias('AU')->join(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group_access as AGA on AGA.uid=AU.uid','left')->join(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group as AG on AG.id=AGA.group_id','left')->field('AU.uid,AU.username as name,AU.email,AU.nick_name,AU.lastloginip,AU.lastlogintime,AU.personnel_no,AU.realname,AU.score,AG.title,AG.id as groupId')->where($map)->order($sort.' '.$order)->cache('authAserListB',30)->select();
        }
		// echo $m->getlastsql();
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
        
        $this->ajaxReturn($data);
    }

    /**
     * ajax检查用户名是否存在
     * @author 黄药师 <46914685@qq.com> [20141210]
     */
    /* public function checkUser(){
        if(!IS_POST) exit;
		$m=M('Warehouse');
		// $n=M('AuthUser',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        $map['username']=$ck->in('用户名称','val','cnennumstr','',5,11);  
        $result=$m->where($map)->find(); 
        if($result){
            $return['status']=false;
            $return['id']=$result['uid'];
        }else{
            $return['status']=true;
            $return['id']='0';
        }
        $this->ajaxReturn($return);        
    } */
	public function checkUser(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');  
        $map['username']=$ck->in('用户名称','val','cnennumstr','',2,11);  
        $result=M()->table(C('DB_DEPOT').'.'.C('DB_PRS').'auth_user')->where($map)->cache('authUser'.$map['username'],600)->find(); 
        if($result) {
            $return['status']=false;
            $return['id']=$result['uid'];
        }else {
            $return['status']=true;
            $return['id']='0';
        }
        $this->ajaxReturn($return);        
    }
    /**
     * ajax检查角色组是否存在
     * @author 黄药师 <46914685@qq.com> [20141210]
     */
    public function checkGroup(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');  
        $map['title']=$ck->in('角色名称','val','cnennumstr','',2,10);  
        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group')->where($map)->cache('authGroup'.$map['title'],600)->find(); 
        if($result) {
            $return['status']=false;
            $return['id']=$result['id'];
        }else {
            $return['status']=true;
            $return['id']='0';
        }
        $this->ajaxReturn($return);
        
    }
    /**
     * ajax检查规则是否存在
     * @author 黄药师 <46914685@qq.com> [20141210]
     */
    public function checkRule(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');  
        $map['name']=$ck->in('规则标识','val','string','',1,80);  
        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_rule')->where($map)->cache('authRule'.$map['name'],600)->find(); 
        if($result) {
            $return['status']=false;
            $return['id']=$result['id'];
        }else {
            $return['status']=true;
            $return['id']='0';
        }
        $this->ajaxReturn($return);        
    }
	
    /**
     * ajax检查邮箱是否存在
     * @author 黄药师 <46914685@qq.com> [20150119]
     */
    public function checkEmail(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');  
        $map['email']=$ck->in('邮箱','val','string','',5,32);  
        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_user')->where($map)->cache('authUser'.$map['email'],600)->find(); 
        if($result) {
            $return['status']=false;
            $return['id']=$result['uid'];
        }else {
            $return['status']=true;
            $return['id']='0';
        }
        $this->ajaxReturn($return);
    }

    //添加管理员
/*     public function userAdd(){
        if(!IS_POST) exit; 
		$m=M('Warehouse');
		$n=M('AuthUser',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');  
		$username=session('username'); 
		$warehouse_code=session('warehouse_code'); 
		
        $data['username']=$ck->in('用户名','name','cnennumstr','',5,11);   
        $data['password']=$ck->in('密码','pwd','password','',5,20); 
        $data['realname']=$ck->in('姓名','realname','cnennumstr','',0,16); 
        $data['email']=$ck->in('邮箱','email','string','',0,32); 
		$data['personnel_no']=$ck->in('员工编号','personnel_no','string','',0,32); 
		$data['warehouse_code']=session('warehouse_code'); 
		$data['db_name']=C('DB_DEPOT.DB_NAME'); 
		$curr_info=$m->where(array('warehouse_code'=>$warehouse_code,'username'=>$username))->find();
		$data['name']=$curr_info['name'];
		$data['warehouse_cate']=$curr_info['warehouse_cate'];
		// $data['DB_HOST']=C('DB_DEPOT.DB_HOST'); 
		// $data['DB_USER']=C('DB_DEPOT.DB_USER'); 
		// $data['DB_PWD']=C('DB_DEPOT.DB_PWD'); 
		// $data['db_port']=C('DB_DEPOT.DB_PORT'); 
		// $data['DB_PRS']=C('DB_PRS');
        $score=$ck->in('积分','score','intval','',0,0); 
        !empty($score)?$data['score']=$score:'';
        $g['group_id']=$ck->in('会员组id','group','intval','',1,10); 
        $insertId=$m->add($data);
		
        if(!$insertId){
            $return['message']='添加用户失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
			unset($data['db_name']);
			unset($data['name']);
			unset($data['warehouse_cate']);
			unset($data['warehouse_code']);
			$insertIds=$n->add($data);
			// echo $n->getlastsql();
			// die;
			if(!$insertIds){
				$return['message']='云端添加用户成功，客户端添加用户失败';
				$return['status']=false;
				$this->ajaxReturn($return);
			}
            $g['uid']=$insertIds;
            $gid=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group_access')->add($g);
            if(!$gid){
               $return['message']='添加用户明细表出错!';
               $return['status']=false; 
               $this->ajaxReturn($return);
            }
        }
        $return['message']='添加用户成功!';
        $return['status']=true; 
        $this->ajaxReturn($return);
    } */

    //更新用户
    /* public function userSave(){
       if(!IS_POST) exit; 
	    $m=M('Warehouse');
	    $n=M('AuthUser',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');     
        $pwd=$ck->in('密码','pwd','password','',0,16);
        !empty($pwd)?$data['password']=$pwd:''; 
        $data['realname']=$ck->in('姓名','realname','cnennumstr','',0,16); 
        $data['email']=$ck->in('邮箱','email','string','',0,32); 
        $data['score']=$ck->in('积分','score','intval','',0,0); 
		$data['personnel_no']=$ck->in('员工编号','personnel_no','string','',0,32); 
        $map['uid']=$ck->in('用户id','uid','intval','',1,0); 
        $g['group_id']=$ck->in('会员组id','groupId','intval','',1,0); 
		
		$user_info=$n->where($map)->find();
		$maps['username']=$user_info['username'];
		$result=$m->where($maps)->save($data);
        if(false===$result){
            $return['message']='更新用户失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
			$results=$n->where($map)->save($data);
            if($map['uid']!='1'){//非超级管理员
                $gid=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group_access')->where($map)->save($g);
                if(false===$gid){
                   $return['message']='更新用户明细表出错!';
                   $return['status']=false; 
                   $this->ajaxReturn($return);
                }
            }
        }
        $return['message']='更新用户成功!';
        $return['status']=true; 
        $this->ajaxReturn($return);
    } */
	/**
	 * 验证输入的手机号码是否合法
	 */
	public function is_mobile_phone($mobile_phone)
	{
		$chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$/";
		if (preg_match($chars, $mobile_phone)) {
			return true;
		}
		return false;
	}
	//添加管理员
    public function userAdd(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');	
        $data['username']=trim(I('name'));
		if(!$this->is_mobile_phone($data['username'])){
			$return['message']='手机号错误';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
		$data['nick_name']=$ck->in('昵称','nick_name','ennumstr','',5,20);
        $data['password']=$ck->in('密码','pwd','password','',5,20); 
        $data['realname']=$ck->in('姓名','realname','cnennumstr','',0,16); 
		$data['personnel_no']=$ck->in('员工编号','personnel_no','string','',0,32); 
        $data['email']=$ck->in('邮箱','email','string','',0,32); 
        
		$map['username']=$data['username'];
		$map['nick_name']=$data['nick_name'];
		$map['_logic']="or";
		if(M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_user')->where($map)->count()){
			$return['message']='手机号或昵称已经存在！';
            $return['status']=false;
            $this->ajaxReturn($return);
		}
		
        $g['group_id']=$ck->in('会员组id','group','intval','',1,10); 
        $insertId=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_user')->add($data);
		
        if(!$insertId){
            $return['message']='添加用户失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $g['uid']=$insertId;
            $gid=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group_access')->add($g);
            if(!$gid){
               $return['message']='添加用户明细表出错!';
               $return['status']=false; 
               $this->ajaxReturn($return);
            }
        }
        $return['message']='添加用户成功!';
        $return['status']=true; 
        $this->ajaxReturn($return);

    }

    //更新用户
    public function userSave(){
       if(!IS_POST) exit; 
        $ck=A('CheckInput');     
        $pwd=$ck->in('密码','pwd','password','',5,20);
        !empty($pwd)?$data['password']=$pwd:''; 
		
        $data['realname']=$ck->in('姓名','realname','cnennumstr','',0,16); 
        $data['email']=$ck->in('邮箱','email','string','',0,32); 
        $data['score']=$ck->in('积分','score','intval','',0,0); 
		$data['personnel_no']=$ck->in('员工编号','personnel_no','string','',0,32); 
        $map['uid']=$ck->in('用户id','uid','intval','',1,0); 
        $g['group_id']=$ck->in('会员组id','groupId','intval','',1,0); 

        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_user')->where($map)->save($data);
        if(false===$result){
            $return['message']='更新用户失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            if($map['uid']!='1'){//非超级管理员
                $gid=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group_access')->where($map)->save($g);
                if(false===$gid){
                   $return['message']='更新用户明细表出错!';
                   $return['status']=false; 
                   $this->ajaxReturn($return);
                }
            }
        }
        $return['message']='更新用户成功!';
        $return['status']=true; 
        $this->ajaxReturn($return);
    }
    /**
     *批量移动用户
     * @author 黄药师 20150125
     */
    public function userMove(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');     
        $uid=$ck->in('用户id','uid','string','',1,0);
        $data['group_id']=$ck->in('角色id','groupId','intval','',1,0);
        if(empty($uid)||empty($data['group_id'])) exit;//为空
        $arr=explode(',', $uid);
        if(in_array('1', $arr)) exit;//id=1是超级管理员,不能移动
        $map['uid']=array('in',$arr);
        $a=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group_access')->where($map)->save($data);
        if(false===$a){
            $return['message']='移动用户失败';
            $return['status']=false;
        }else{
            $return['message']='移动用户成功';
            $return['status']=true;
        }
        $this->ajaxReturn($return);
    }

    //删除用户
    /* public function userDel(){
        if(!IS_POST) exit; 
		$m=M('Warehouse');
	    $n=M('AuthUser',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');  
        $uid=$ck->in('用户id','id','string','',1,0);
        if(empty($uid)) exit;//为空
        $arr=explode(',', $uid);
        if(in_array('1', $arr)) exit;//id=1是超级管理员,不能删除
        $map['uid']=array('in',$arr);

		$user_info=$n->where($map)->find();
		$maps['username']=$user_info['username'];
		$maps['warehouse_code']=session('warehouse_code');
        $result=$m->where($maps)->delete();
        if(!$result){
            $return['message']='删除用户失败!';
            $return['status']=false;  
        }else{
			$results=$n->where($map)->delete();
            $a=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group_access')->where($map)->delete();
            if(!$a){
                $return['message']='删除用户明细表失败!';
                $return['status']=false; 
            }else{
                $return['message']='删除用户成功!';
                $return['status']=true; 
            }
        }        
        $this->ajaxReturn($return);
    } */
	public function userDel(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');  
        $uid=$ck->in('用户id','id','string','',1,0);
        if(empty($uid)) exit;//为空
        $arr=explode(',', $uid);
        if(in_array('1', $arr)) exit;//id=1是超级管理员,不能删除
        $map['uid']=array('in',$arr);
        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_user')->where($map)->delete();
        if(!$result){
            $return['message']='删除用户失败!';
            $return['status']=false;  
        }else{
            $a=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group_access')->where($map)->delete();
            if(!$a){
                $return['message']='删除用户明细表失败!';
                $return['status']=false; 
            }else{
                $return['message']='删除用户成功!';
                $return['status']=true; 
            }
        }        
        $this->ajaxReturn($return);
    }
    //规则列表
    public function ruleList(){
        if(!IS_AJAX) exit;
		$ms=M('AuthRule',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
        $name='';
        $title='';
        $name=$ck->in('规则标识','name','string','',0,80);
        $title=$ck->in('规则描述','title','string','',0,20);
        !empty($name)?$map['name']=array('like','%'.$name.'%'):'';
        !empty($title)?$map['title']=$title:'';

        $map['status']=1;
        $page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);

        $count=$ms->where($map)->count();

        $m=$ms->where(array('status'=>'1'))->field('id,pid,title')->select();//获取模块名
		
		foreach ($m as $k=>$v) {
			$module[$v['id']]=$v['title'];
        }
        
        $info=$ms->where($map)->page($page,$rows)->order('pid,o')->select();
		
        foreach ($info as $k=>$v) {
			if(!$v['pid']){
				$info[$k]['moduleName']="顶级菜单";
			}else{
				$info[$k]['moduleName']=$module[$v['pid']];
			}
        }
		foreach ($info as $k=>$v) {
			if($v['pid']==0){
				$arr[]=$v;
				foreach($info as $key=>$value){
					if($value['pid']==$v['id']){
						$value['title']="&nbsp;&nbsp;|".$value['title'];
						$arr[]=$value;
						foreach($info as $keys=>$values){
							if($values['pid']==$value['id']){
								$values['title']="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||".$values['title'];
								$arr[]=$values;
							}
						}
					}
				}
			}
        }
// dump($arr);
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$arr;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
        //dump($data);        
        $this->ajaxReturn($data);
    }

	//添加规则
	public function ruleAdd(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');  
		
        $data['contr']=$ck->in('控制器','contr','string','',2,50); 
		$data['title']=$ck->in('规则描述','title','string','',0,50); 
		$data['func']=$ck->in('方法','func','string','',2,50); 
        $data['name']=MODULE_NAME.'/'.$data['contr'].'/'.$data['func'];
        $data['condition']=$ck->in('附加条件','condition','string','',0,100); 
        $data['status']=$ck->in('状态','status','intval','',0,100); 
		$data['icon']=$ck->in('icon图标','icon','string','',0,100);
		$data['is_menu']=$ck->in('菜单','is_menu','intval','',0,1);
		$data['o']=$ck->in('排序','o','intval','',0,1); 
        $data['pid']=$ck->in('所属模块','pid','intval','',1,0); 
        //规则标识存在
        if(M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_rule')->where(array('name'=>$data['name']))->cache('ruleAdd'.$data['name'],60)->find()) exit;

        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_rule')->add($data);
        if(!$result){
            $return['message']='添加规则失败!';
            $return['status']=false;  
        }else{
            $return['message']='添加规则成功!';
            $return['status']=true; 
        }        
        $this->ajaxReturn($return);
	}

    //更新规则
    public function ruleSave(){
		$edit=I('edit');
		$ck=A('CheckInput');  
		if($edit==1){//快速编辑
			 //
		}else{
			 $data['title']=$ck->in('规则描述','title','string','',0,50); 
			 $data['status']=$ck->in('状态','status','intval','',0,1); 
			 $data['pid']=$ck->in('所属模块','pid','intval','',1,0); 
		}
        
        $data['contr']=$ck->in('控制器','contr','string','',0,50); 
		$data['func']=$ck->in('方法','func','string','',0,50); 
		$data['name']=MODULE_NAME.'/'.$data['contr'].'/'.$data['func'];
        $data['condition']=$ck->in('附加条件','condition','string','',0,100); 
		$data['icon']=$ck->in('icon图标','icon','string','',0,100);
		$data['is_menu']=$ck->in('菜单','is_menu','intval','',0,1); 
		$data['o']=$ck->in('排序','o','intval','',0,100); 
        
        $map['id']=$ck->in('规则id','id','intval','',1,0);

        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_rule')->where($map)->save($data);
		// echo M()->getlastsql();
        if(false===$result){
            $return['message']='更新规则失败!';
            $return['status']=false;  
        }else{
            $return['message']='更新规则成功!';
            $return['status']=true; 
        }        
        $this->ajaxReturn($return);
    }

    /**
     * 删除规则
     * 
     * @author 有冇有 <46914685@qq.com> [20150122]
     */
    public function ruleDel(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');  
        $id=$ck->in('规则id','id','string','',1,0);
        $map['id']=array('in',$id);
        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_rule')->where($map)->delete();
        if(!$result){
            $return['message']='删除规则失败!';
            $return['status']=false;  
        }else{
            $return['message']='删除规则成功!';
            $return['status']=true;
        }        
        $this->ajaxReturn($return);
    }

    //角色列表
    public function groupList(){
        $ck=A('CheckInput');
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);

        $count=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group')->cache('authGroupListA',30)->count();
        $info=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group')->order('id desc')->page($page,$rows)->select();

        $data['total']=$count;
        $data['rows']=$info;
        $this->ajaxReturn($data);
    }

    //添加角色
    public function groupAdd(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');  
        $data['title']=$ck->in('角色名称','title','cnennumstr','',2,10);   
        $data['describe']=$ck->in('角色描述','describe','cnennumstr','',0,16); 
        $data['status']=$ck->in('状态','status','intval','',0,1); 
        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group')->add($data);
        if(!$result){
            $return['message']='添加角色失败!';
            $return['status']=false;  
        }else{
            $return['message']='添加角色成功!';
            $return['status']=true;
            S('authGroupListA',null);//清除缓存
            S('authGroupListB',null);//清除缓存

        }        
        $this->ajaxReturn($return);
    }

    //删除角色
    public function groupDel(){
       if(!IS_POST) exit; 
        $ck=A('CheckInput');  
        $map['id']=$ck->in('状态','id','intval','',1,0);
        if($map['id']=='1') exit;//超级管理员不能删除
        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group')->where($map)->delete();
        if(!$result){
            $return['message']='删除角色失败!';
            $return['status']=false;  
        }else{
            $a_=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group_access')->where(array('group_id'=>$map['id']))->save(array('group_id'=>'2'));//切换用户到默认组,防止出错
            if(false===$a_){
                $return['message']='更新用户明细表失败!';
                $return['status']=false;
            }else{
                $return['message']='删除角色成功!';
                $return['status']=true;
                S('authGroupListA',null);//清除缓存
                S('authGroupListB',null);//清除缓存
            }
        }        
        $this->ajaxReturn($return);
    }
	//删除配置
    public function baseConfigdel(){
       if(!IS_POST) exit; 
        $ck=A('CheckInput'); 
		$ids=I('id');		
        $map['id']=array('in',$ids);
        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'config')->where($map)->delete();
        if(!$result){
            $return['message']='删除失败!';
            $return['status']=false;  
        }else{
            $return['message']='删除成功!';
            $return['status']=true;
        }        
        $this->ajaxReturn($return);
    }

    //更新角色组
    public function groupSave(){
        if(!IS_POST) exit; 
        $ck=A('CheckInput');  
        $data['title']=$ck->in('角色名称','title','cnennumstr','',2,10);   
        $data['describe']=$ck->in('角色描述','describe','cnennumstr','',0,16); 
        $data['status']=$ck->in('状态','status','intval','',0,1); 
        $map['id']=$ck->in('id','id','intval','',0,0); 
        if($map['id']=='1'&&$map['id']=='2') exit;//超级管理员和默认组不能修改
        $result=M()->table(C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'auth_group')->where($map)->save($data);
        if(false===$result){
            $return['message']='更新角色失败!';
            $return['status']=false;  
        }else{
            $return['message']='更新角色成功!';
            $return['status']=true; 
            S('authGroupListA',null);//清除缓存
            S('authGroupListB',null);//清除缓存
        }        
        $this->ajaxReturn($return);        
    }

    /**
     *权限设置列表 
     * 
     */
    public function accessList(){
		$m=M('AuthRule',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('AuthGroup',C('DB_PRS'),C('DB_DEPOT'));

        // $id=(int)I('id');//父级id
        $gid=(int)I('gid');//角色组id
		
		$rule_list=$m->where(array('status'=>1))->select();
		foreach($rule_list as $keyss=>$valuess){
			$info_list=$m->where(array('status'=>1,'pid'=>$valuess['id']))->select();//查找子列表
			if($info_list){
				$arr_list[]=$valuess['id'];//有子列表的id记录下来
			}
		}
		
        // $map['pid']=$id?$id:0;
        $rules=$n->where(array('id'=>$gid))->getField('rules');//角色组rules
		$rules_arr=explode(',',$rules);
		// dump($rules);
		// dump($arr_list);
		foreach($rules_arr as $kks=>$vvs){
			foreach($arr_list as $kkks=>$vvvs){
				if($vvs==$vvvs){
					unset($rules_arr[$kks]);
					continue;
				}
			}
		}
		$rules=implode(',',$rules_arr);
        $data=$m->field('id,title as text,pid as parentId,state')->where(array('pid'=>0))->order('id,o')->select();
		// dump($rules_arr);
		// die;
		foreach($data as $k=>$v){
			$second_list=$m->field('id,title as text,pid as parentId,state')->where(array('pid'=>$v['id']))->select();
			foreach($second_list as $key=>$value){
				$three_list=$m->field('id,title as text,pid as parentId,state')->where(array('pid'=>$value['id']))->select();
				$second_list[$key]['children']=$three_list;
			}
			$data[$k]['state']='closed';
			$data[$k]['children']=$second_list;
		}
        if(!empty($rules)){
            $arr=explode(',', $rules);
            foreach ($data as $ks=>$vs) {
                if(in_array($vs['id'], $arr)){
                    $vs['checked']=true;
                    $data[$ks]=$vs;
                }
				foreach ($vs['children'] as $kss=>$vss) {
					if(in_array($vss['id'], $arr)){
						$vss['checked']=true;
						$data[$ks]['children'][$kss]=$vss;
					}
					foreach ($vss['children'] as $keys=>$values) {
						if(in_array($values['id'], $arr)){
							$values['checked']=true;
							$data[$ks]['children'][$kss]['children'][$keys]=$values;
						}

					}
				}
            }
        }
		// dump($data);
		// die;
        $this->ajaxReturn($data);
    }

    //更新角色拥有的规则
    public function AccessSet(){
		$n=M('AuthGroup',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');  
		
        $data['rules']=$ck->in('权限列表','rules','string','',0,0);

        $map['id']=$ck->in('角色组id','id','intval','',1,0);
        if($map['id']=='1'&&empty($map['id'])) exit;//超级管理员或空退出

        $result=$n->where($map)->save($data);
        if(false===$result){
            $return['message']='权限设置失败!';
            $return['status']=false;
        }else{
            $return['message']='权限设置成功!';
            $return['status']=true;
        }        
        $this->ajaxReturn($return);
    } 
}
?>